<template>
  <div class="_fd-drag-tool" :class="{ active: fcx.active === id }" @click.stop="active">
    <div v-if="mask" class="_fd-drag-mask"></div>
    <div v-if="!hiddenBtn" class="_fd-drag-l">
      <div v-if="fcx.active === id && dragBtn !== false" class="_fd-drag-btn" style="cursor: move">
        <i class="fc-icon icon-move"></i>
      </div>
    </div>
    <div v-if="btns !== false && !hiddenMenu" class="drag-r">
      <slot name="handle">
        <div v-if="isCreate && (btns === true || btns.indexOf('create') > -1)" class="_fd-drag-btn" @click="$emit('create')">
          <i class="fc-icon icon-add"></i>
        </div>
        <div v-if="!only && (btns === true || btns.indexOf('copy') > -1)" class="_fd-drag-btn" @click="$emit('copy')">
          <i class="fc-icon icon-copy"></i>
        </div>
        <div v-if="children && (btns === true || btns.indexOf('addChild') > -1)" class="_fd-drag-btn" @click="$emit('addChild')">
          <i class="fc-icon icon-add-child"></i>
        </div>
        <div v-if="btns === true || btns.indexOf('delete') > -1" class="_fd-drag-btn _fd-drag-danger" @click="$emit('delete')">
          <i class="fc-icon icon-delete"></i>
        </div>
      </slot>
    </div>
    <slot name="default"></slot>
  </div>
</template>

<script>
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'DragTool',
  inject: {
    fcx: {
      default: null
    },
    designer: {
      default: null
    },
    dragTool: {
      default: null
    }
  },
  provide() {
    return {
      dragTool: this
    }
  },
  props: {
    dragBtn: Boolean,
    children: String,
    mask: Boolean,
    handleBtn: [Boolean, Array],
    formCreateInject: Object,
    unique: String,
    only: Boolean
  },
  emits: ['create', 'copy', 'addChild', 'delete', 'active', 'fc.el'],
  computed: {
    isCreate() {
      return this.dragTool ? !!this.dragTool.children : false
    },
    btns() {
      if (Array.isArray(this.handleBtn)) {
        return this.handleBtn.length ? this.handleBtn : false
      }
      return this.handleBtn !== false
    },
    id() {
      return this.unique || this.formCreateInject.id
    },
    hiddenMenu() {
      return this.designer.ctx.hiddenDragMenu
    },
    hiddenBtn() {
      return this.designer.ctx.hiddenDragBtn
    }
  },
  mounted() {
    this.$emit('fc.el', this)
  },
  methods: {
    active() {
      if (this.fcx.active === this.id) return
      this.fcx.active = this.id
      this.$emit('active')
    }
  }
})
</script>

<style>
._fd-drag-tool {
  position: relative;
  display: block;
  min-height: 20px;
  box-sizing: border-box;
  padding: 2px;
  overflow: hidden;
  word-wrap: break-word;
  word-break: break-all;
  transition: outline-color 0.3s ease;
}
._fd-drag-tool:hover {
  outline: 1px dashed var(--fc-tool-border-color);
}

._fd-drag-tool ._fd-drag-tool {
  height: calc(100% - 6px);
  margin: 3px;
}

._fd-drag-tool + ._fd-drag-tool {
  margin-top: 5px;
}

._fd-drag-tool.active {
  outline: 2px solid #2e73ff;
}

._fd-drag-tool.active > div > ._fd-drag-btn {
  display: flex;
}

._fd-drag-tool._fd-drop-hover ._fd-drag-box {
  padding-top: 15px !important;
  padding-bottom: 15px !important;
}

._fd-drag-tool ._fd-drag-btn {
  display: none;
}

.drag-r {
  position: absolute;
  right: 2px;
  bottom: 2px;
  z-index: 94;
}

._fd-drag-l {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 99;
}

._fd-drag-btn {
  height: 18px;
  width: 18px;
  color: #fff;
  background-color: #2e73ff;
  text-align: center;
  line-height: 20px;
  padding-bottom: 1px;
  float: left;
  cursor: pointer;
  justify-content: center;
}

._fd-drag-btn + ._fd-drag-btn {
  margin-left: 2px;
}

._fd-drag-danger {
  background-color: #ff2e2e;
}

._fd-drag-btn i {
  font-size: 14px;
}

._fd-drag-mask {
  z-index: 90;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
</style>
